#!/bin/bash

# Request Body 可重复读取测试脚本
# 使用方法: chmod +x test-api.sh && ./test-api.sh

BASE_URL="http://localhost:8080"

echo "=================================="
echo "Request Body 可重复读取测试"
echo "=================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试1: 健康检查
echo -e "${BLUE}[测试 1]${NC} 健康检查"
echo "curl -X GET ${BASE_URL}/health"
echo "---"
curl -s -X GET ${BASE_URL}/health | jq .
echo ""
echo ""

# 测试2: 基础重复读取
echo -e "${BLUE}[测试 2]${NC} 基础重复读取（同时用 @RequestBody 和 手动读取）"
echo "curl -X POST ${BASE_URL}/demo -H 'Content-Type: application/json' -d '{\"name\":\"kimi\",\"age\":18}'"
echo "---"
curl -s -X POST ${BASE_URL}/demo \
     -H "Content-Type: application/json" \
     -d '{"name":"kimi","age":18}' | jq .
echo ""
echo -e "${YELLOW}💡 查看控制台日志，应该看到两次读取的内容完全一致${NC}"
echo ""
echo ""

# 测试3: 多组件读取场景
echo -e "${BLUE}[测试 3]${NC} 模拟多组件读取（日志切面 + 参数校验 + 业务逻辑）"
echo "curl -X POST ${BASE_URL}/multi-read -H 'Content-Type: application/json' -d '{\"action\":\"test\",\"data\":\"multiple-reads\"}'"
echo "---"
curl -s -X POST ${BASE_URL}/multi-read \
     -H "Content-Type: application/json" \
     -d '{"action":"test","data":"multiple-reads"}' | jq .
echo ""
echo -e "${YELLOW}💡 查看控制台日志，应该看到三次读取的内容完全一致${NC}"
echo ""
echo ""

# 测试4: 复杂 JSON
echo -e "${BLUE}[测试 4]${NC} 复杂 JSON 结构"
echo "curl -X POST ${BASE_URL}/demo -H 'Content-Type: application/json' -d '{...}'"
echo "---"
curl -s -X POST ${BASE_URL}/demo \
     -H "Content-Type: application/json" \
     -d '{
       "user": {
         "id": 12345,
         "name": "张三",
         "email": "zhangsan@example.com"
       },
       "metadata": {
         "timestamp": "2025-10-01T10:30:00Z",
         "source": "mobile-app"
       },
       "items": [
         {"productId": "P001", "quantity": 2},
         {"productId": "P002", "quantity": 5}
       ]
     }' | jq .
echo ""
echo ""

# 测试5: 中文内容
echo -e "${BLUE}[测试 5]${NC} 中文内容测试（验证编码正确性）"
echo "curl -X POST ${BASE_URL}/demo -H 'Content-Type: application/json' -d '{...}'"
echo "---"
curl -s -X POST ${BASE_URL}/demo \
     -H "Content-Type: application/json; charset=UTF-8" \
     -d '{"标题":"SpringBoot 请求体缓存","内容":"支持重复读取，完美解决一次性流的问题！","作者":"Artisan"}' | jq .
echo ""
echo ""

echo "=================================="
echo -e "${GREEN}✅ 所有测试完成！${NC}"
echo "=================================="
echo ""
echo "📋 验证清单："
echo "  □ 健康检查返回 ok"
echo "  □ 基础重复读取 consistent: true"
echo "  □ 多组件读取 allConsistent: true"
echo "  □ 复杂 JSON 解析正确"
echo "  □ 中文内容无乱码"
echo ""
echo "📝 查看详细日志："
echo "  tail -f logs/spring-boot-logger.log"
echo "  或查看控制台输出"

